package com.gxa.controller.dmt.attendance;


import com.gxa.common.R;

import com.gxa.entity.AttendanceClock;
import com.gxa.service.AttendanceService;
import com.gxa.utils.HeaderUtils;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * wgl
 */
@Controller
@RequestMapping("/attendance/attend")
@RequiresRoles(value = {"超级管理员","院长","主任","秘书"},logical = Logical.OR)
@Api(value = "考勤管理", tags = "考勤管理接口API")
public class AttendanceController {

    @Autowired
    AttendanceService service;

    @ApiOperation(value = "显示所有考勤列表信息", notes = "显示所有考勤列表信息",
            httpMethod = "GET", response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页码数默认第一页", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", dataType = "String", required = false)
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @RequestMapping(value = "/findAttendList", method = RequestMethod.GET)
    @ResponseBody
    public R findAttendList(@RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize) {

        R r = service.findAttendList(currentPage,pageSize);

        return r;
    }


    /**
     * 根据考勤Id删除考勤
     * @param id 考勤Id
     * @return
     */
    @ApiOperation(value = "根据考勤Id删除考勤", notes = "根据考勤Id删除考勤",
            httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "考勤Id", paramType = "query", dataType = "String", required = true)
    })
    @RequestMapping(value = "/delAttendById", method = RequestMethod.POST)
    @ResponseBody
    public R delAttendById(@RequestParam("id") Integer id) {

        R r = service.delAttendById(id);

        return r;
    }


    /**
     * 根据条件查询考勤列表信息
     *
     * @param currentPage 当前页码数默认第一页
     * @param pageSize    每页显示记录数
     * @param condition        tiaojian
     * @return
     */
    @ApiOperation(value = "根据类型条件查询考勤列表信息", notes = "根据类型条件查询考勤列表信息",
            httpMethod = "GET", response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页码数默认第一页", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "type", value = "类型条件", paramType = "query", dataType = "String", required = false)
    })
    @RequestMapping(value = "/findAttendByCondition", method = RequestMethod.GET)
    @ResponseBody
    public R findAttendByType(@RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize, @RequestParam("condition") String condition) {

        R r = service.findAttendListByCondition(currentPage,pageSize,condition);
        return r;
    }


    /**
     * 根据考勤Id修改考勤信息
     *
     * @param id   考勤Id
     * @param memo 考勤备注
     * @return
     */
    @ApiOperation(value = "根据考勤Id修改考勤信息", notes = "根据考勤Id修改考勤信息",
            httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "考勤Id", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "memo", value = "考勤备注", paramType = "query", dataType = "String", required = true)
    })
    @RequestMapping(value = "/updateAttend", method = RequestMethod.POST)
    @ResponseBody
    public R updateAttend(@RequestParam("id") Integer id, @RequestParam("memo") String memo) {
        R result = service.updateAttendance(id,memo);
        return result;
    }


    /**
     * 根据ID回显考勤信息
     *
     * @param id 考勤Id
     * @return
     */
    @ApiOperation(value = "根据ID回显考勤信息", notes = "根据ID回显考勤信息",
            httpMethod = "GET", response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "考勤Id", paramType = "query", dataType = "String", required = true)
    })
    @RequestMapping(value = "/findAttendById", method = RequestMethod.GET)
    @ResponseBody
    public R findAttendById(@RequestParam("id") Integer id) {

        R result = service.findAttendById(id);

        return result;
    }

    /**
     * 添加考勤信息
     *
     * @param attendanceClock 考勤实体
     * @return
     */

    @ApiOperation(value = "添加考勤信息", notes = "添加考勤信息",
            httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @RequestMapping(value = "/addAttend", method = RequestMethod.POST)
    @ResponseBody
    public R addAttend(@ApiParam(name = "attendanceClock", value = "考勤实体", required = true) @RequestBody AttendanceClock attendanceClock,
                       HttpServletRequest request) {

        Integer userID = HeaderUtils.getUserID(request);

        //获取登录的ip地址
        String remoteAddr = request.getRemoteAddr();
        attendanceClock.setIp(remoteAddr);
//        //获取登录用户的用户id
        attendanceClock.setUserId(userID);

        R result = service.addAttendance(attendanceClock);

        return result;
    }
}
